﻿Imports System.Drawing

Public Class ImageBuffer
    Private Buff As New List(Of Bitmap)
    Private M_Limit As Integer = 10
    Private m_Level As Integer = 0
    Public Event BufferOverflow()
    Public Event BufferLevel(ByVal Level As Integer)
    Public Event BufferImageAdded()
    Private m_Lock As Boolean = False
    Public Sub New()
        M_Limit = 10
    End Sub
    Public Sub New(ByVal Size As Integer)
        M_Limit = Size
    End Sub
    Public Sub Add(ByVal Bmp As Bitmap)
        If m_Lock = False Then
            If Buff.Count < M_Limit Then
                Buff.Add(New Bitmap(Bmp))
                m_Level = (100 / M_Limit) * Buff.Count
                RaiseEvent BufferLevel(m_Level)
                RaiseEvent BufferImageAdded()
                If Buff.Count = M_Limit Then RaiseEvent BufferOverflow()
            Else
                Buff.RemoveAt(0)
                Buff.Add(New Bitmap(Bmp))
                m_Level = (100 / M_Limit) * Buff.Count
                RaiseEvent BufferLevel(m_Level)
                RaiseEvent BufferImageAdded()
                If Buff.Count = M_Limit Then RaiseEvent BufferOverflow()
            End If
        End If
    End Sub
    Public Function GetBmp(Optional ByVal Remove As Boolean = False) As Bitmap
        If Buff.Count > 0 Then
            Return Buff.ElementAt(0)
            If Remove Then Buff.RemoveAt(0)
        Else
            Return Nothing
        End If
    End Function
    Public Sub Clear()
        Buff.Clear()
        RaiseEvent BufferLevel(0)
    End Sub
    Public Property BufferSize() As Integer
        Get
            Return M_Limit
        End Get
        Set(ByVal value As Integer)
            M_Limit = value
        End Set
    End Property
    Public ReadOnly Property BufferFull() As Boolean
        Get
            If Buff.Count = M_Limit Then
                Return True
            Else
                Return False
            End If
        End Get
    End Property
    Public ReadOnly Property BufferEmpty() As Boolean
        Get
            If Buff.Count = 0 Then
                Return True
            Else
                Return False
            End If
        End Get
    End Property
    Public ReadOnly Property Locked() As Boolean
        Get
            Return m_Lock
        End Get
    End Property
    Public Sub Lock()
        m_Lock = True
    End Sub
    Public Sub UnLock()
        m_Lock = False
    End Sub
End Class
